package com.situ.hospital.dao.Impl;

import com.situ.hospital.dao.IUserDAO;
import com.situ.hospital.pojo.User;
import com.situ.hospital.pojo.query.UserQuery;
import com.situ.hospital.pojo.vo.UserVO;
import com.situ.hospital.util.JDBCUtils;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;

public class UserDAOImpl implements IUserDAO {

    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<UserVO> selectByPage(UserQuery userQuery) {
        String sql = "SELECT " +
                "u.id,u.`name`,u.password,u.image,u.age,u.gender,d.`name` AS departmentName,u.status,u.role,u.create_time,u.update_time " +
                "FROM user AS u LEFT JOIN department AS d ON u.department_id=d.id WHERE 1=1";
        List<Object> queryList = new ArrayList<>();
        String queryName = userQuery.getName();
        if(queryName != null && !"".equals(queryName)) {
            sql += " AND u.`name` LIKE ?";
            queryList.add("%" + queryName + "%");
        }
        String queryGender = userQuery.getGender();
        if(queryGender != null && !"".equals(queryGender)) {
            sql += " AND u.gender LIKE ?";
            queryList.add("%" + queryGender + "%");
        }
        Integer queryDepartmentId = userQuery.getDepartmentId();
        if(queryDepartmentId != null) {
            sql += " AND u.department_id=?";
            queryList.add(queryDepartmentId);
        }
        sql += " LIMIT ?,?";
        int offset = (userQuery.getPage() - 1) * userQuery.getLimit();
        queryList.add(offset);
        queryList.add(userQuery.getLimit());
        return template.query(sql, new BeanPropertyRowMapper<>(UserVO.class), queryList.toArray());
    }

    @Override
    public int selectTotalCount(UserQuery userQuery) {
        String sql = "SELECT COUNT(*) FROM user WHERE 1=1";
        List<Object> queryList = new ArrayList<>();
        String queryName = userQuery.getName();
        if(queryName != null && !"".equals(queryName)) {
            sql += " AND `name` LIKE ?";
            queryList.add("%" + queryName + "%");
        }
        String queryGender = userQuery.getGender();
        if(queryGender != null && !"".equals(queryGender)) {
            sql += " AND gender LIKE ?";
            queryList.add("%" + queryGender + "%");
        }
        Integer queryDepartmentId = userQuery.getDepartmentId();
        if(queryDepartmentId != null) {
            sql += " AND department_id=?";
            queryList.add(queryDepartmentId);
        }
        return template.queryForObject(sql, Integer.class, queryList.toArray());
    }

    @Override
    public void deleteById(int id) {
        String sql = "DELETE FROM user WHERE id=?";
        template.update(sql, id);
    }

    @Override
    public void add(User user) {
        String sql = "INSERT INTO user(name,image,age,gender,role) VALUES(?,?,?,?,?)";
        template.update(sql, user.getName(), user.getImage(), user.getAge(), user.getGender(), user.getRole());
    }

    @Override
    public User selectById(int id) {
        String sql = "SELECT id,name,password,image,age,gender,department_id,status,role,create_time,update_time FROM user WHERE id=?";
        return template.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), id);
    }

    @Override
    public void update(User user) {
        String sql = "UPDATE user SET name=?,password=?,age=?,gender=?,department_id=?,role=?,image=? WHERE id=?";
        template.update(sql, user.getName(), user.getPassword(), user.getAge(), user.getGender(), user.getDepartmentId(),user.getRole(),user.getImage(), user.getId());
    }

    @Override
    public void updateStatus(int id, int status) {
        String sql = "UPDATE user SET status=? WHERE id=?";
        template.update(sql, status,id);
    }

    @Override
    public User login(String name, String password) {
        try {
            String sql = "SELECT id,`name`,password,role,image,status FROM user WHERE name=? AND password=?";
            return template.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), name, password);
        } catch (EmptyResultDataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<User> selectUser() {
        String sql = "SELECT id,`name`,password,image,age,gender,department_id,status,role,create_time,update_time " +
                "FROM user WHERE status=? AND department_id!=?";
        return template.query(sql, new BeanPropertyRowMapper<>(User.class), 1, 1);
    }
}
